/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cplex;

import ilog.concert.*;
import ilog.cplex.IloCplex;

/**
 *
 * @author stkr
 */
public class BinPacking {
    
    public static void main(String[] args){
        try{
            IloCplex cplex = new IloCplex();
            
            IloNumVar[] var = new IloNumVar[3];
            IloRange[] rng  = new IloRange[3];
            
            populateByColumn(cplex, var, rng);
            
            if(cplex.solve()) {
                cplex.output().println("Successfully solved.");
                
                cplex.output().println("Solution status = " + cplex.getStatus());
                cplex.output().println("Solution value  = " + cplex.getObjValue());
                
                double[] x  = cplex.getValues(var);
                double[] pi = cplex.getDuals(rng);
                
                for (int j = 0; j < 3; j++){
                    cplex.output().println("Value of Variable " + j + " = " + x[j]);
                }
                
                for (int j = 0; j < 3; j++){
                    cplex.output().println("Value of Dual Variable " + j + " = " + pi[j]);
                }
            }
        }
        catch (IloException e) {
         System.err.println("Concert exception '" + e + "' caught");
    }
    
    }
        static void populateByColumn (IloMPModeler model,
                                IloNumVar[] var,
                                IloRange[] rng) throws IloException {
            IloObjective cost = model.addMinimize();
            
            rng[0] = model.addRange(4,Double.MAX_VALUE, "c1");
            rng[1] = model.addRange(2,Double.MAX_VALUE, "c2");
            rng[2] = model.addRange(6,Double.MAX_VALUE, "c3");
            
            IloColumn col1 = model.column(cost,   1.0).and(
                             model.column(rng[0], 1.0).and(
                             model.column(rng[1], 0.0).and(
                             model.column(rng[2], 0.0))));
            IloColumn col2 = model.column(cost,   1.0).and(
                             model.column(rng[0], 0.0).and(
                             model.column(rng[1], 2.0).and(
                             model.column(rng[2], 1.0))));
            IloColumn col3 = model.column(cost,   1.0).and(
                             model.column(rng[0], 0.0).and(
                             model.column(rng[1], 0.0).and(
                             model.column(rng[2], 4.0))));
            
            var[0] = model.numVar(col1, 0.0, Double.MAX_VALUE, "x1");
            var[1] = model.numVar(col2, 0.0, Double.MAX_VALUE, "x2");
            var[2] = model.numVar(col3, 0.0, Double.MAX_VALUE, "x3");
        }

}